home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVEPSN.C < prev    next >
C/C++ Source or Header  |  1991-11-27  |  9KB  |  254 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevepsn.c */
  21. /* Epson dot-matrix printer driver for Ghostscript */
  22. #include "gdevprn.h"
  23. /************************************************
  24.  * For 9-pin printers, you may select
  25.  *   X_DPI = 60, 120, or 240
  26.  *   Y_DPI = 60 or 72
  27.  * For 24-pin printers, you may select
  28.  *   X_DPI = 60, 120, 180, 240, or 360
  29.  *   Y_DPI = 60, 72, 180, or 216
  30.  * Note that a given printer implements *either* Y_DPI = 60 | 180 *or*
  31.  * Y_DPI = 72 | 216; no attempt is made to check this here.
  32.  * Note that X_DPI = 180 or 360 requires Y_DPI > 100;
  33.  * this isn't checked either.  Finally, note that X_DPI=240 and
  34.  * X_DPI=360 are double-density modes requiring two passes to print.
  35.  *
  36.  * The values of X_DPI and Y_DPI may be set at compile time:
  37.  * see gdevs.mak.
  38.  * 
  39.  * At some time in the future, we could simulate 24-bit output on
  40.  * 9-pin printers by using fractional vertical positioning;
  41.  * we could even implement an X_DPI=360 mode by using the
  42.  * ESC++ command that spaces vertically in units of 1/360"
  43.  * (not supported on many printers.)
  44.  ************************************************/
  45.  
  46. #ifndef X_DPI
  47. #  define X_DPI 240            /* pixels per inch */
  48. #endif
  49. #ifndef Y_DPI
  50. #  define Y_DPI 60            /* pixels per inch */
  51. #endif
  52.  
  53. /* The device descriptors */
  54. private dev_proc_print_page(eps_print_page);
  55. gx_device_printer gs_epson_device =
  56.   prn_device(prn_std_procs, "epson",
  57.     85,                /* width_10ths, 8.5" */
  58.     110,                /* height_10ths, 11" */
  59.     X_DPI, Y_DPI,
  60.     0, 0, 0.5, 0,        /* margins */
  61.     1, eps_print_page);
  62.  
  63. /* ------ Internal routines ------ */
  64.  
  65. /* Forward references */
  66. private void eps_output_run(P6(byte *, int, int, char, FILE *, int));
  67.  
  68. /* Send the page to the printer. */
  69. #define DD 0x80                /* double density flag */
  70. private int
  71. eps_print_page(gx_device_printer *pdev, FILE *prn_stream)
  72. {    static char graphics_modes_9[5] =
  73.        {    -1, 0 /*60*/, 1 /*120*/, -1, DD+3 /*240*/
  74.        };
  75.     static char graphics_modes_24[7] =
  76.        {    -1, 32 /*60*/, 33 /*120*/, 39 /*180*/,
  77.         -1, -1, DD+40 /*360*/
  78.        };
  79.     int y_24pin = pdev->y_pixels_per_inch > 72;
  80.     int y_mult = (y_24pin ? 3 : 1);
  81.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  82.     int in_size = line_size * (8 * y_mult);
  83.     byte *in = (byte *)gs_malloc(in_size, 1, "eps_print_page(in)");
  84.     int out_size = ((pdev->width + 7) & -8) * y_mult;
  85.     byte *out = (byte *)gs_malloc(out_size, 1, "eps_print_page(out)");
  86.     int x_dpi = pdev->x_pixels_per_inch;
  87.     char start_graphics =
  88.         (y_24pin ? graphics_modes_24 : graphics_modes_9)[x_dpi / 60];
  89.     int first_pass = (start_graphics & DD ? 1 : 0);
  90.     int last_pass = first_pass * 2;
  91.     int dots_per_space = x_dpi / 10;    /* pica space = 1/10" */
  92.     int bytes_per_space = dots_per_space * y_mult;
  93.     int skip = 0, lnum = 0, pass;
  94.  
  95.     /* Check allocations */
  96.     if ( in == 0 || out == 0 )
  97.        {    if ( in ) gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  98.         if ( out ) gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  99.         return -1;
  100.        }
  101.  
  102.     /* Initialize the printer and reset the margins. */
  103.     fwrite("\033@\033P\033l\000\r\033Q", 1, 10, prn_stream);
  104.     fputc((int)(pdev->width / pdev->x_pixels_per_inch * 10) + 2,
  105.           prn_stream);
  106.  
  107.     /* Print lines of graphics */
  108.     while ( lnum < pdev->height )
  109.        {    byte *inp = in;
  110.         byte *in_end = in + line_size;
  111.         byte *out_end = out;
  112.         byte *out_blk;
  113.         register byte *outp;
  114.         int lcnt;
  115.  
  116.         /* Copy 1 scan line and test for all zero. */
  117.         gdev_prn_copy_scan_lines(pdev, lnum, in, line_size);
  118.         if ( in[0] == 0 &&
  119.              !memcmp((char *)in, (char *)in + 1, line_size - 1)
  120.            )
  121.            {    lnum++;
  122.             skip += 3 / y_mult;
  123.             continue;
  124.            }
  125.  
  126.         /* Vertical tab to the appropriate position. */
  127.         while ( skip > 255 )
  128.            {    fputs("\033J\377", prn_stream);
  129.             skip -= 255;
  130.            }
  131.         if ( skip )
  132.             fprintf(prn_stream, "\033J%c", skip);
  133.  
  134.         /* Copy the rest of the scan lines. */
  135.         lcnt = 1 + gdev_prn_copy_scan_lines(pdev, lnum + 1, in + line_size, in_size - line_size);
  136.         if ( lcnt < 8 * y_mult )
  137.           memset(in + lcnt * line_size, 0,
  138.              in_size - lcnt * line_size);
  139.  
  140.         /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  141.         /* because that's how the printer wants the data. */
  142.         /* If we are in a 24-pin mode, we have to transpose */
  143.         /* groups of 3 lines at a time. */
  144.  
  145.         if ( y_24pin )
  146.          { for ( ; inp < in_end; inp++, out_end += 24 )
  147.             { gdev_prn_transpose_8x8(inp, line_size, out_end, 3);
  148.               gdev_prn_transpose_8x8(inp + line_size * 8, line_size, out_end + 1, 3);
  149.               gdev_prn_transpose_8x8(inp + line_size * 16, line_size, out_end + 2, 3);
  150.             }
  151.            /* Remove trailing 0s. */
  152.            while ( out_end > out && out_end[-1] == 0 &&
  153.                out_end[-2] == 0 && out_end[-3] == 0
  154.              )
  155.              out_end -= 3;
  156.          }
  157.         else
  158.          { for ( ; inp < in_end; inp++, out_end += 8 )
  159.             { gdev_prn_transpose_8x8(inp, line_size, out_end, 1);
  160.             }
  161.            /* Remove trailing 0s. */
  162.            while ( out_end > out && out_end[-1] == 0 )
  163.              out_end--;
  164.          }
  165.  
  166.         for ( pass = first_pass; pass <= last_pass; pass++ )
  167.            {
  168.         for ( out_blk = outp = out; outp < out_end; )
  169.          { /* Skip a run of leading 0s. */
  170.            /* At least 10 are needed to make tabbing worth it. */
  171.            /* We do everything by 3's to avoid having to make */
  172.            /* different cases for 9- and 24-pin. */
  173.  
  174.            if ( *outp == 0 && outp + 12 <= out_end &&
  175.             outp[1] == 0 && outp[2] == 0 &&
  176.             (outp[3] | outp[4] | outp[5]) == 0 &&
  177.             (outp[6] | outp[7] | outp[8]) == 0 &&
  178.             (outp[9] | outp[10] | outp[11]) == 0
  179.               )
  180.             {    byte *zp = outp;
  181.             int tpos;
  182.             byte *newp;
  183.             outp += 12;
  184.             while ( outp + 3 <= out_end && *outp == 0 &&
  185.                 outp[1] == 0 && outp[2] == 0
  186.                   )
  187.                 outp += 3;
  188.             tpos = (outp - out) / bytes_per_space;
  189.             newp = out + tpos * bytes_per_space;
  190.             if ( newp > zp + 10 )
  191.              { /* Output preceding bit data. */
  192.                if ( zp > out_blk )    /* only false at */
  193.                         /* beginning of line */
  194.                  eps_output_run(out_blk, (int)(zp - out_blk),
  195.                         y_mult, start_graphics,
  196.                         prn_stream, pass);
  197.                /* Tab over to the appropriate position. */
  198.                fprintf(prn_stream, "\033D%c%c\t", tpos, 0);
  199.                out_blk = outp = newp;
  200.              }
  201.            }
  202.           else
  203.             outp += y_mult;
  204.          }
  205.         if ( outp > out_blk )
  206.             eps_output_run(out_blk, (int)(outp - out_blk),
  207.                        y_mult, start_graphics,
  208.                        prn_stream, pass);
  209.  
  210.         fputc('\r', prn_stream);
  211.            }
  212.         skip = 24;
  213.         lnum += 8 * y_mult;
  214.        }
  215.  
  216.     /* Eject the page and reinitialize the printer */
  217.     fputs("\f\033@", prn_stream);
  218.     fflush(prn_stream);
  219.  
  220.     gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  221.     gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  222.     return 0;
  223. }
  224.  
  225. /* Output a single graphics command. */
  226. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  227. private void
  228. eps_output_run(byte *data, int count, int y_mult,
  229.   char start_graphics, FILE *prn_stream, int pass)
  230. {    int xcount = count / y_mult;
  231.     fputc(033, prn_stream);
  232.     if ( !(start_graphics & ~3) )
  233.        {    fputc("KLYZ"[start_graphics], prn_stream);
  234.        }
  235.     else
  236.        {    fputc('*', prn_stream);
  237.         fputc(start_graphics & ~DD, prn_stream);
  238.        }
  239.     fputc(xcount & 0xff, prn_stream);
  240.     fputc(xcount >> 8, prn_stream);
  241.     if ( !pass )
  242.         fwrite(data, 1, count, prn_stream);
  243.     else
  244.        {    /* Only write every other column of y_mult bytes. */
  245.         int which = pass;
  246.         byte *dp = data;
  247.         register int i, j;
  248.         for ( i = 0; i < xcount; i++, which++ )
  249.           for ( j = 0; j < y_mult; j++, dp++ )
  250.            {    putc(((which & 1) ? *dp : 0), prn_stream);
  251.            }
  252.        }
  253. }
  254.